package com.dd.census.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dd.census.entity.SysRole;
import com.dd.census.service.impl.SysRoleServiceImpl;
import com.dd.census.tools.TokenHelper;
import com.dd.census.tools.resopnse.ResultResponse;
import com.dd.census.tools.resopnse.emums.ResultStatus;

@RestController
@RequestMapping(value = "census/sysRole")
public class SysRoleController {

	private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);

	@Autowired
	private SysRoleServiceImpl sysRoleService;

	@RequestMapping("select")
	public ResultResponse<Object> select(@RequestBody SysRole sysRole, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(sysRoleService.selectPage(sysRole));

		}  catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResultResponse<Object> insert(HttpServletRequest request, @RequestBody SysRole sysRole) {

		try {
			// 插入数据
			sysRole.setCreateBy(TokenHelper.getUserId(request));
			return ResultResponse.buildSuccess(sysRoleService.insert(sysRole));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResultResponse<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {

			return ResultResponse.buildSuccess(sysRoleService.selectById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("update")
	public ResultResponse<Object> update(HttpServletRequest request, @RequestBody SysRole sysRole) {

		try {
			// 修改数据
			sysRole.setUpdateBy(TokenHelper.getUserId(request));
			return ResultResponse.buildSuccess(sysRoleService.update(sysRole));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

}
